package com.kkwrite.regimen.consumer.controller.param;

import com.kkwrite.regimen.common.entity.dto.OutDTO;
import com.kkwrite.regimen.consumer.controller.constant.Constant;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * Created by lisha on 2018/7/22 11:18.
 *
 * @author lisha
 */
@RestController
@Api(value = "参数模块")
public class ParamController {

    @Resource(name = "restTemplate")
    private RestTemplate restTemplate;

    @GetMapping(value = "/param/query/{type}")
    @HystrixCommand(fallbackMethod = "queryParamError")
    @ApiImplicitParams(
        {
            @ApiImplicitParam(name = "type", value = "参数类型", required = true, dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "name", value = "参数名", dataType = "string", paramType = "query")
        }
    )
    public OutDTO queryParam(@PathVariable Integer type, @RequestParam(required = false) String name) {
        // 入参校验
        if (type == null || type < 0) {
            return new OutDTO("false", "入参不合法");
        }
        String url = Constant.SERVICE_PREFIX + "/param/queryParam?type=" + type;
        if (!StringUtils.isBlank(name)) {
            url += "&name=" + name;
        }
        return restTemplate.getForObject(url, OutDTO.class);
    }

    public OutDTO queryParamError(Integer type, String name) {
        return new OutDTO("false", "服务异常！");
    }

}
